#include "daemon.h"
#include <signal.h>

//See Steven's book at page 418.
int Daemon::init ( )
{
	pid_t pid ;

	if ( (pid = fork()) < 0 )
	{
		pDLog->writeLog("fork process error.\n") ;
		return -1 ;
	}	
	else if ( pid != 0 )
	{
		pDLog->writeLog("Parent process exit gracefully.\n") ;
		exit ( 0 ) ; //parent goes bye-bye
	}

	//Child continues
	pDLog->writeLog("Sub process starts running.\n") ;
	
	setsid ( ) ; //become session leader

	//close STDOUT, STDIN, STDERR
	for ( int i=0; i < 3; i++ )
		close ( i ) ;
		
	chdir ("/") ; //change working directory
	
	umask (0) ; //clear our file mode creation mask

	return 0 ;
}

void Daemon::start ( void * routineParm )
{
	dRoutine ( routineParm ) ;
}
